import {createSql} from "./index";
import type {Park, Tree} from "../types";

const tableName = "t_park";

export const queryList = () =>
    createSql<Park[]>('SELECT * FROM ' + tableName + ' ORDER BY `rank` ASC');

export async function getRank(){
  const ParkList = await createSql<Park[]>('SELECT * FROM ' + tableName + ' ORDER BY `rank` DESC LIMIT 1')
  return ParkList.length > 0 ? ParkList[0].rank + 1 : 1;
}

export const insert = async (data: Park) =>{
  return  createSql<Park[]>(
      'INSERT INTO '+ tableName + ' (id, name, status ,type ,price,createTime,`rank`) VALUES ( ?,?,?,?,?,?,?)',
      [data.id, data.name, data.status,data.type ,data.price,data.createTime,data.rank]
  );
}
export const deleteById = (id: string) =>{
  return createSql<Park[]>(' DELETE FROM '+ tableName + ' WHERE id = ?', [id]);
}

export const update = async (data: Park) =>{
  return createSql<Park[]>(
      'UPDATE '+ tableName + ' SET  status = ?, endTime = ? WHERE id = ?',
      [data.status,data.endTime, data.id]
  );
}

export const queryById = (id) =>
    createSql<Park[]>('SELECT * FROM ' + tableName + ' WHERE id = ?',[id]);

export const queryNotice = () =>
    createSql<Park[]>('SELECT * FROM '+ tableName +' ORDER BY rank DESC LIMIT 1');

export const queryListOnlyFinish = () =>
    createSql<Park[]>('SELECT * FROM ' + tableName + ' WHERE status = 0 ORDER BY `rank` ASC ');
export default {
  insert,
  queryList,
  deleteById,
  getRank,
  update,
  queryById,
  queryNotice,
  queryListOnlyFinish
}
